<?php
namespace app\api\controller;

use app\common\exception\FailException;
use app\common\controller\Base;
use app\common\exception\ParameterException;
use app\common\logic\Agent as AgentLogic;
use think\Loader;
use think\Request;

/**
 * @title 代理商模块接口
 * @description 接口说明
 * @group 接口分组
 * @header name:token require:1 default: desc:token
 * @param name:user_type type:int require:1 default:1 other: desc:状态(1.会员登陆，2.商家，3.代理商，4.省级代理商，5.内部员工)
 */
class Agent extends Base
{
    /**
     * @title 代理商团队接口
     * @description 接口说明
     * @author WCJ
     * @url /api/agent/agent_team
     * @method POST
     *
     *
     * @return id: 自增ID
     *
     *
     */
  public function agent_team(Request $request){

      if($request->isPost()){
          $userid=$this->uid;
          if($this->type==3){

             $agentlogic = new AgentLogic();
             $data=$agentlogic->get_team($userid);
              $datas= ["code"=>200,"msg"=>"成功","data"=>$data];
              if($this->new_token){
                  $datas["new_token"]=$this->new_token;
              }
              return $datas;

          }else{

              throw new FailException(["code"=>201,"msg"=>"登录账户不是代理商账号!","errorCode"=>404]);
          }


      }

  }

    /**
     * @title 二级代理商详情接口
     * @description 接口说明
     * @author WCJ
     * @url /api/agent/team_info
     * @method POST
     * @param name:uid type:int require:1 default:1 other: desc:二级代理ID
     *
     * @return id: 自增ID
     *
     *
     */
    public function team_info(Request $request){

        if($request->isPost()){
            $uid = $request->post("uid");
            if($this->type==3){

                $agentlogic = new AgentLogic();
                $data=$agentlogic->team_info($uid);
                $datas= ["code"=>200,"msg"=>"成功","data"=>$data];
                if($this->new_token){
                    $datas["new_token"]=$this->new_token;
                }
                return $datas;

            }else{

                throw new FailException(["code"=>201,"msg"=>"登录账户不是代理商账号!","errorCode"=>404]);
            }


        }

    }
  
      /**
     * @title 二级会员详情接口
     * @description 接口说明
     * @author WCJ
     * @url /api/agent/team_userinfo
     * @method POST
     * @param name:uid type:int require:1 default:1 other: desc:二级代理ID
     *
     * @return id: 自增ID
     *
     *
     */
    public function team_userinfo(Request $request){

        if($request->isPost()){
            $uid = $request->post("uid");
            if($this->type==3){

                $agentlogic = new AgentLogic();
                $data=$agentlogic->team_info2($uid);
                $datas= ["code"=>200,"msg"=>"成功","data"=>$data];
                if($this->new_token){
                    $datas["new_token"]=$this->new_token;
                }
                return $datas;

            }else{

                throw new FailException(["code"=>201,"msg"=>"登录账户不是代理商账号!","errorCode"=>404]);
            }


        }

    }

    /**
     * @title 代理商中心接口
     * @description 接口说明
     * @author WCJ
     * @url /api/agent/agent_center
     * @method POST
     *
     * @return id: 自增ID
     *
     *
     */
    public function agent_center(Request $request){

        if($request->isPost()){
            $userid=$this->uid;
            if($this->type==3){
                $agentlogic = new AgentLogic();
                $data=$agentlogic->agent_center($userid);
                $datas= ["code"=>200,"msg"=>"成功","data"=>$data];
                if($this->new_token){
                    $datas["new_token"]=$this->new_token;
                }
                return $datas;

            }else{

                throw new FailException(["code"=>201,"msg"=>"登录账户不是代理商账号!","errorCode"=>404]);
            }


        }

    }

    /**
     * @title 代理商记录接口
     * @description 接口说明
     * @author WCJ
     * @url /api/agent/agent_log
     * @method POST
     * @param name:start type:int require:1 default:1 other: desc:开始数
     * @param name:num type:int require:1 default:1 other: desc:数量
     * @param name:start_time type:int require:0 default:1 other: desc:开始时间
     * @param name:end_time type:int require:0 default:20 other: desc:结束时间
     *
     * @return id: 自增ID
     *
     *
     */
    public function agent_log(Request $request){

        if($request->isPost()){
            $userid=$this->uid;
            if($this->type==3){
                $start=$request->post("start")?$request->post("start"):1;
                $num=$request->post("num")?$request->post("num"):20;

                $start_time = $request->post("start_time");
                $end_time = $request->post("end_time");
                $agentlogic = new AgentLogic();
                $data=$agentlogic->get_agent_log($userid,$start,$num,$start_time,$end_time);
                $datas= ["code"=>200,"msg"=>"成功","data"=>$data[0],"totle"=>$data[1]];
                if($this->new_token){
                    $datas["new_token"]=$this->new_token;
                }
                return $datas;

            }else{

                throw new FailException(["code"=>201,"msg"=>"登录账户不是代理商账号!","errorCode"=>404]);
            }


        }

    }

    /**
     * @title 代理商设置接口
     * @description 接口说明
     * @author WCJ
     * @url /api/agent/set_agent
     * @method POST
     * @param name:uid type:int require:1 default:1 other: desc:二级代理ID
     * @param name:status type:int require:1 default:1 other: desc:1开启，0关闭
     * @param name:agent_bl type:int require:1 default:1 other: desc:比例
     * @param name:agent_name type:string require:1 default:1 other: desc:代理商姓名备注
     *
     * @return id: 自增ID
     *
     *
     */
    public function set_agent(Request $request){

        if($request->isPost()){
            $userid=$this->uid;
            if($this->type==3){

                $uid = $request->post("uid");
                $status = $request->post("status")?$request->post("status"):0;
                $agent_bl=$request->post("agent_bl")?$request->post("agent_bl"):0;
                $agent_name=$request->post("agent_name");
                $validate = Loader::validate("User");
                if (!$validate->scene("name")->check(['name' => $agent_name])) {
                    $ret=['code' => 201, 'msg' => $validate->getError(),'errorCode' => 404];
                    throw new ParameterException($ret);
                }
                $agentlogic = new AgentLogic();
                $data=$agentlogic->set_agent($userid,$uid,$status,$agent_bl,$agent_name);
                $datas= ["code"=>200,"msg"=>"成功","data"=>$data];
                if($this->new_token){
                    $datas["new_token"]=$this->new_token;
                }
                return $datas;

            }else{

                throw new FailException(["code"=>201,"msg"=>"登录账户不是代理商账号!","errorCode"=>404]);
            }


        }

    }





}